﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceLoadBalancer.Client;
using ResourceLoadBalancer.WebServices.Clients.Helpers;
using ResourceLoadBalancer.WebServices.Contracts.Data;
using System.IO;
using System.Net;
using System.Security.AccessControl;
using Microsoft.SharePoint;


namespace FileDuplicateFinderHasher
{
    public class SharePointHasher:FileHasher
    {
        private static int BATCH_SIZE = 100;
        private static string SHAREPOINT_QUEUE_TYPE = "SharePoint";

        public SharePointHasher()
        {

        }

        public void DeconfigureQueuing(string queueName)
        {
            this.queueName = queueName;
            base.DeconfigureQueuing(SHAREPOINT_QUEUE_TYPE, queueName);
        }

        public void ProcessResourceQueue(string queueName)
        {
            this.queueName = queueName;
            SharePointURIDataContract queuedItem = null;
            queuedItem = serviceClient.GetQueuedSharePointResourceURI();

            while (queuedItem.DocumentURL != null)
            {

                HashResource(queuedItem);
                Console.WriteLine(queuedItem.DocumentURL);
                queuedItem = serviceClient.GetQueuedSharePointResourceURI();

                if (Resources.Count == BATCH_SIZE)
                {
                    SubmitResources(BATCH_SIZE);
                }

                if (ResourcesURI.Count == BATCH_SIZE)
                {
                    SubmitResourceURIs(BATCH_SIZE);
                }

            }
        }

        public void HashResource(SharePointURIDataContract sharePointResourceURI)
        {
            MD5Utility md5Utility = new MD5Utility();


            ResourceURIDataContract resourceURI = new ResourceURIDataContract();

            if (sharePointResourceURI.SiteCollectionURL != null &&
                sharePointResourceURI.WebURL != null &&
                sharePointResourceURI.DocumentURL != null)
            {
                SPSite siteCollection = new SPSite(sharePointResourceURI.SiteCollectionURL);
                SPWeb web = siteCollection.OpenWeb(sharePointResourceURI.WebURL);
                SPFile file = web.GetFile(sharePointResourceURI.DocumentURL);

                string fullUrl = sharePointResourceURI.DocumentURL;


                try
                {
                    ResourceDataContract resource = md5Utility.CalculateMD5Hash(file.OpenBinary());

                    resourceURI.ResourceHash = resource.Hash;
                    resourceURI.URI = fullUrl;
                    resourceURI.HostName = hostName;
                    resourceURI.URIHash = md5Utility.CalculateMD5Hash(file.Url);
                    resourceURI.Owner = file.ModifiedBy.LoginName;
                    resourceURI.LastModifiedDate = file.TimeLastModified;
                    resourceURI.CreatedDate = file.TimeCreated;
                    resourceURI.InventoryDate = DateTime.Now;


                    if (resource.Hash != "Unable to access file")
                        Resources.Add(resource);

                    if (resourceURI.ResourceHash != "Unable to access file")
                        ResourcesURI.Add(resourceURI);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception was encountered for the URL: " + fullUrl);
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    siteCollection.Dispose();
                    web.Dispose();
                }
            }
            
        }
    }
}
